package mrpheu.demo.client.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketTimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class UDPIOImpl {
    private static final int TIME_OUT = 5000;
    
    public static Object connect(DatagramSocket socketServer, DatagramPacket packet) {
        if(socketServer == null) {
            return null;
        }
        int count = 0;
        while(count <= 3) {
            try {
                System.out.println("Sending...");
                socketServer.send(packet);
                socketServer.setSoTimeout(TIME_OUT);
                while(true) {
                    try {
                        byte[] buf = new byte[512];
                        DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
                        socketServer.receive(receivePacket);
                        System.out.println("Send ack.");
                        return receivePacket;
                    } catch (SocketTimeoutException e) {
                        count++;
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    
    public static boolean sendData(DatagramSocket socketServer, DatagramPacket packet) {
        try {
            socketServer.send(packet);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            return false;
        }
        return true;
//        int count = 0;
//        while(count <= 3) {
//            System.out.println(count);
//            try {
//                socketServer.send(packet);
//                System.out.println("Sending..." + count);
//                socketServer.setSoTimeout(TIME_OUT);
//                while(true) {
//                    try {
//                        //System.out.println("Waiting ack...");
//                        byte[] buf = new byte[512];
//                        DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
//                        socketServer.receive(receivePacket);
//                        //socketServer.send(packet);
//                        System.out.println("Send success.");
//                        return true;
//                    } catch (SocketTimeoutException e) {
//                        count++;
//                        break;
//                    }
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//                System.out.println("IO problem!");
//                return false;
//            }
//        }
//        return false;
    }

    public static Object receiveData(DatagramSocket socketServer) {
        if(socketServer == null) {
            return null;
        }
        byte[] buf = new byte[512];
        DatagramPacket packetReceive = new DatagramPacket(buf, buf.length);
        try {
            socketServer.receive(packetReceive);
            System.out.println("Nhan tu server thanh cong.");
            byte[] dataByte = packetReceive.getData();
            String dataStr = new String(dataByte);
            System.out.println("Receiver: " + dataStr);
        } catch (IOException e) {
            return null;
        }
        byte[] buf1 = new byte[512];
        DatagramPacket packetAck = new DatagramPacket(buf1, buf1.length);
//        try {
//            //socketServer.send(packetAck);
//        } catch (IOException e) {
//        }
        return packetReceive;
    }
    
}
